<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    width="500px"
    @callBack="onCallBack"
    @close="onClose"
  >
    <el-form
      ref="ruleForm"
      :model="form"
      :rules="rules"
      label-width="80px"
      label-position="left"
    >
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="form.name" clearable placeholder="请输入任务名称" />
      </el-form-item>

      <el-form-item label="参数内容" prop="map">
        <el-input
          type="textarea"
          rows="4"
          v-model="form.map"
          clearable
          placeholder="请输入参数内容"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOne, save } from "@/api/sysDataxConfig";

export default {
  data() {
    return {
      dialogVisible: false,
      title: "添加",
      form: {
        id: "",
        name: "",
        cron: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur",
          },
        ],
        map: [
          {
            required: true,
            message: "请输入参数内容",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init() {
      getOne(this.form.id).then((res) => {
        if (res && res.code === 20000) {
          const rtn = res.data;
          this.form.id = rtn.id;
          this.form.name = rtn.name;
          this.form.map = rtn.map;
        }
      });
    },
    onShow(id) {
      if (id) {
        this.form.id = id;
        this.title = "编辑";
        this.init();
      } else {
        this.title = "添加";
      }
      this.dialogVisible = true;
    },
    onClose() {
      this.$refs["ruleForm"].resetFields();
      this.form = this.$options.data().form;
      this.dialogVisible = false;
    },
    onSubmit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          save(this.form)
            .then((res) => {
              if (res && res.code === 20000) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.dialogVisible = false;
                this.onCallBack();
              }
            })
            .catch((e) => {});
        } else {
          return false;
        }
      });
    },
    onCallBack() {
      this.$emit("onCallBack");
    },
  },
};
</script>

<style scoped>
.el-form {
  padding-left: 60px;
  padding-right: 60px;
}
</style>
